//堆栈柱状图
function stackbar(domId, title, subtitle, xAxis, yAxis, series, unit) {
  $('#' + domId).highcharts({
    chart: {
      type: 'column'
    },
    title: {
      text: title,
      style: {
        fontWeight: 'bold'
      }
    },
    subtitle: {
      text: subtitle
    },
    xAxis: {
      categories: xAxis
    },
    yAxis: {
      min: 0,
      title: {
        text: yAxis
      },
      stackLabels: {
        enabled: true,
        style: {
          fontWeight: 'bold',
          color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
        }
      }
    },
    legend: {
      align: 'right',
      x: -70,
      verticalAlign: 'top',
      y: 20,
      floating: true,
      backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
      borderColor: '#CCC',
      borderWidth: 1,
      shadow: false
    },
    tooltip: {
      formatter: function() {
        return '<b>' + this.x + '</b><br/>' +
          this.series.name + ': ' + this.y + '<br/>' +
          unit + this.point.stackTotal;
      }
    },
    plotOptions: {
      column: {
        stacking: 'normal',
        dataLabels: {
          enabled: true,
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'black'
        }
      }
    },
    series: series,
    colors: ['#3599EE', '#27B3C3'],
    lang: {
      noData: '暂无数据'
    },
    noData: {
      style: {
        fontSize: '20px',
        color: '#303030'
      }
    }
  });
  $('text:contains("Highcharts.com")').empty();
}
//饼状图初始化
function pie(domId, title, subtitle, seriesname, series, enableprint) {
  $('#' + domId).highcharts({
    chart: {
      plotBackgroundColor: null,
      plotBorderWidth: null,
      plotShadow: false
    },
    title: {
      text: title,
      style: {
        fontWeight: 'bold'
      }
    },
    subtitle: {
      text: subtitle,
      x: -20
    },
    tooltip: {
      pointFormat: '{series.name}: <b>{point.y}</b>'
    },
    plotOptions: {
      pie: {
        allowPointSelect: true,
        cursor: 'pointer',
        dataLabels: {
          enabled: true,
          color: '#000000',
          connectorColor: '#000000',
          format: '<b>{point.name}</b>: {point.percentage:.1f} %'
        }
      }
    },
    series: [{
      type: 'pie',
      name: seriesname,
      data: series
    }],
    lang: {
      noData: '暂无数据'
    },
    noData: {
      style: {
        fontSize: '20px',
        color: '#303030'
      }
    },
    exporting: {
      enabled: enableprint
    }
  });
  $('text:contains("Highcharts.com")').empty();

}

//曲线图
function linear(domId, title, subtitle, xAxis, yAxis, tooltip, series, enableprint) {
  $('#' + domId).highcharts({
    title: {
      text: title,
      x: -20 //center
        ,
      style: {
        fontWeight: 'bold'
      }
    },
    subtitle: {
      text: subtitle,
      x: -20
    },
    xAxis: {
      categories: xAxis
    },
    yAxis: {
      title: {
        text: yAxis
      },
      min: 0,
      plotLines: [{
        value: 0,
        width: 1,
        color: '#000000'
      }]
    },
    tooltip: {
      formatter: function() {
        return '<b>' + this.series.name + ': ' + Highcharts.numberFormat(this.y, 2, '.') + tooltip + '<b>';
      }
    },
    legend: {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'middle',
      borderWidth: 0
    },
    series: series,
    lang: {
      noData: '暂无数据'
    },
    noData: {
      style: {
        fontSize: '20px',
        color: '#303030'
      }
    },
    exporting: {
      enabled: enableprint
    }
  });
  $('text:contains("Highcharts.com")').empty();

}
//时间轴折线图
function timeLinear(domId, title, subtitle, xAxis, yAxis, series, date) {

  $('#' + domId).highcharts({
    chart: {
      zoomType: 'x',
      spacingRight: 20
    },
    title: {
      text: title
    },
    subtitle: {
      text: subtitle
    },
    xAxis: {
      type: 'datetime',
      dateTimeLabelFormats: {
        day: '%m/ %d',
        week: '%m/ %d',
        month: '%Y/%m'
      },
      title: {
        text: null
      }
    },
    yAxis: {
      title: {
        text: yAxis
      },
      min: 0
    },
    tooltip: {
      formatter: function() {
        return Highcharts.dateFormat('%Y-%m-%d', this.x) + '<br/>' + yAxis + this.y + '<br/>'
      }

    },
    legend: {
      enabled: false
    },
    plotOptions: {
      area: {
        fillColor: {
          linearGradient: {
            x1: 0,
            y1: 0,
            x2: 0,
            y2: 1
          },
          stops: [
            [0, Highcharts.getOptions().colors[0]],
            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
          ]
        },
        lineWidth: 1,
        marker: {
          enabled: false
        },
        shadow: false,
        states: {
          hover: {
            lineWidth: 1
          }
        },
        threshold: null
      }
    },
    series: [{
      type: 'area',
      name: 'USD to EUR',
      pointInterval: 24 * 3600 * 1000,
      pointStart: date.getTime(), //Date.UTC(2016, 0, 01)
      data: series
    }]
  });
  $('text:contains("Highcharts.com")').empty();
}

//动态折线图
function spline(domId, title, xAxis, yAxis, tooltip, min, increment, seriesInit) {
  Highcharts.setOptions({
    global: {
      useUTC: false
    }
  });
  $('#' + domId).highcharts({
    chart: {
      type: 'spline',
      animation: Highcharts.svg,
      events: {
        load: function() {
          var series = this.series;
          setInterval(function() {
            var x = (new Date()).getTime();
            for (var i = 0; i < seriesInit.length; i++) {
              var serie = series[i];
              if (increment == 0) {
                var y = min[i] + xAxis / 1000 + 0;
              } else {
                var y = min[i] + xAxis / 1000 + Mock.mock('@increment(' + increment + ')');
              }
              serie.addPoint([x, y], true, true);
            }
          }, 5000);
        }
      }
    },
    title: {
      text: title
    },
    xAxis: {
      type: 'datetime',
      tickPixelInterval: 150
    },
    yAxis: {
      title: {
        text: yAxis
      },
      plotLines: [{
        value: 0,
        width: 1,
        color: '#808080'
      }]
    },
    tooltip: {
      formatter: function() {
        return '<b>' + this.series.name + '</b><br/>' +
          Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
          tooltip + Highcharts.numberFormat(this.y, 2);
      }
    },
    legend: {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'middle',
      borderWidth: 0
    },
    exporting: {
      enabled: false
    },
    series: seriesInit

  });
  $('text:contains("Highcharts.com")').empty();
}

function spline2(domId, storeName, thisyear, today) {
  Highcharts.setOptions({
    global: {
      useUTC: false
    }
  });
  $('#' + domId).highcharts({
    chart: {
      type: 'spline',
      animation: Highcharts.svg,
      events: {
        load: function() {
          var series = this.series[1];
          var linexAxis = this.xAxis[0];
          setInterval(function() {
            var xinit = (new Date(today)).getTime();
            var init = series.data.length;
            var lastX = series.data[init - 1].x;
            var lastid = init - 1;
            var now = (new Date()).getTime();
            var y = Math.random() > 0.5 ? 1 : null;
            linexAxis.removePlotLine("plot-line-a" + '');
            var next = lastX + 600000;
            if (next < xinit + 3600000 * 24) {
              linexAxis.addPlotLine({ //在x轴上增加标示线
                value: next, //在值为x的地方
                width: 1, //标示线的宽度为1px
                color: 'red', //颜色
                dashStyle: 'longdashdot', //样式
                id: 'plot-line-' + init + '' //标示线的id，在删除该标示线的时候需要该id标示
              });
              if ((next < xinit + 3600000 * 6) || (next > xinit + 3600000 * 18)) {
                series.addPoint([next, null]);
              } else {
                series.addPoint([next, 1]);
              }
            } //else {
            //    linexAxis.addPlotLine({
            //        value: now,
            //        width: 1,
            //        color: 'red',
            //        dashStyle: 'longdashdot',
            //        id: 'plot-line-a' + ''
            //    });
            //    series.addPoint([now, y]);
            //}
            linexAxis.removePlotLine("plot-line-" + lastid + '');
          }, 100);
        }
      }
    },
    title: {
      text: storeName
    },
    xAxis: {
      type: 'datetime',
      dateTimeLabelFormats: {
        minute: '%H:%M',
        day: '%m/ %d',
        week: '%m/ %d',
        month: '%Y/%m'
      },
      tickPixelInterval: 150
    },
    yAxis: {
      title: {
        text: '开灯状态'
      },
      plotLines: [{
        value: 0,
        width: 1,
        color: '#808080'
      }],
      categories: [0, 1],
      min: 1
    },
    tooltip: {
      formatter: function() {
        return '<b>当前时间</b><br/>' +
          Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x)
      }
    },
    legend: {
      enabled: false
    },
    exporting: {
      enabled: false
    },
    series: [{
      name: 'Random data',
      data: [
        [(new Date(thisyear + ' 00:00:00')).getTime(), 0],
        [(new Date(thisyear + ' 06:00:00')).getTime(), 0],
        [(new Date(thisyear + ' 12:00:00')).getTime(), 0],
        [(new Date(thisyear + ' 18:00:00')).getTime(), 0],
        [(new Date(thisyear + ' 23:59:59')).getTime(), 0]
      ]
    }, {
      name: '用电状态',
      data: [
        [(new Date(today)).getTime(), null]
      ]
    }]
  })
}
